﻿using System;
using System.Drawing;
using BasicFramework;
using BasicFramework.Components;
using BasicFramework.Devices.DirectX9;
using BasicFramework.Rendering.Buffer.DirectX9;
using BasicFramework.Rendering.Primitives.Vertizes;
using BasicFramework.Services;
using BasicFramework.Services.Graphics;
using SlimDX;
using SlimDX.Direct3D9;
using BasicFramework.Rendering.Primitives.Indexes;

namespace BasicFrameworkComponentLibrary.DirectX9Components.HelperComponents
{
    public class Graticule : Component
    {

        #region Fields

        private IGraphicsService<DX9DeviceContext> graphicsService;

        private DX9DynamicVertexBuffer<ColorVertex> vBuffer;
        private DX9DynamicIndexBuffer<Index> iBuffer;
        private float length;

        #endregion


        #region Properties

        public float Length
        {
            get { return length; }
            set { length = value; }
        }

        #endregion


        #region Constructors

        #endregion


        #region Events

        #endregion


        #region Methods

        public override void Initialize(bool createDependencies)
        {
            base.Initialize(createDependencies);

            this.Length = 5;

            this.graphicsService = ServiceProvider.GetService<IGraphicsService<DX9DeviceContext>>();
        }

        public override void Draw(float frame)
        {
            base.Draw(frame);

            if (this.graphicsService != null)
            {
                this.vBuffer.SetBuffer(0);
                this.iBuffer.SetBuffer(0);
                this.graphicsService.DeviceContext.Device.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, 4, 0, 3);
            }

        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (this.vBuffer != null)
            {
                this.vBuffer.Dispose();
            }
            if (iBuffer != null)
            {
                this.iBuffer.Dispose();
            }
        }

        public override void LoadResources(bool loadAllContent)
        {
            base.LoadResources(loadAllContent);

            if (this.graphicsService != null)
            {
                vBuffer = new DX9DynamicVertexBuffer<ColorVertex>(this.graphicsService.DeviceContext);

                iBuffer = new DX9DynamicIndexBuffer<Index>(graphicsService.DeviceContext);

                vBuffer.Add(new ColorVertex(new Vector3(0.0f, 0.0f, 0.0f), Color.Green.ToArgb()));
                vBuffer.Add(new ColorVertex(new Vector3(this.Length, 0.0f, 0.0f), Color.Green.ToArgb()));
                vBuffer.Add(new ColorVertex(new Vector3(0.0f, this.Length, 0.0f), Color.Red.ToArgb()));
                vBuffer.Add(new ColorVertex(new Vector3(0.0f, 0.0f, this.Length), Color.Yellow.ToArgb()));

                vBuffer.Commit();


                iBuffer.Add(new Index(0));
                iBuffer.Add(new Index(1));
                iBuffer.Add(new Index(0));
                iBuffer.Add(new Index(2));
                iBuffer.Add(new Index(0));
                iBuffer.Add(new Index(3));

                iBuffer.Commit();

            }
        }

        public override void UnloadResources(bool unloadAllContent)
        {
            base.UnloadResources(unloadAllContent);

            this.Dispose(true);
        }
        #endregion

    }
}
